import { z } from 'zod'
import fs from 'fs'
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js'
import { completable } from "@modelcontextprotocol/sdk/server/completable.js";

//服务器介绍
const server = new McpServer({
  name: 'guiwu',
  title: 'guiwus mcp server',
  version: '1.0.0',
})

//注册工具
server.registerTool('add', {
  title: 'add something',
  description: 'add something',
  inputSchema: {
    a: z.number().describe('the thing to add'),
    b: z.number().describe('the thing to add')
  },
}, ({ a, b }) => {
  return {
    content: [
      {
        type: 'text',
        text: `两数求和结果：${a + b}`
      }
    ]
  }
})

server.registerTool('saveFile', {
  title: 'save file',
  description: 'save file',
  inputSchema: {
    fileName: z.string().describe('the file name'),
    content: z.string().describe('the file content')
  },
}, ({ fileName, content }) => {
  const desktopPath = '/Users/zengyufan/Desktop/' + fileName
  fs.writeFileSync(desktopPath, content)
  return {
    content: [
      {
        type: 'text',
        text: fileName + '创建成功！'
      }
    ]
  }
})

server.registerTool('getImgUrl', {
  title: 'get img url',
  description: 'get img url from 360 server',
  inputSchema: {
    imgName: z.string().describe('the img name'),
  },
}, ({ imgName }) => {
  return {
    content: [
      {
        type: 'text',
        text: imgName + '获取成功！'
      },
      {
        type: 'text',
        text: 'https://img360.360buyimg.com/n1/s450x450_jfs/t1/132222/37/17139/88223/63717324E43602135/3822233336322343.jpg'
      }
    ]
  }
})

server.registerPrompt(
  "review-code",
  {
    title: "Code Review",
    description: "Review code for best practices and potential issues",
    argsSchema: { code: z.string() }
  },
  ({ code }) => ({
    messages: [{
      role: "user",
      content: {
        type: "text",
        text: `Please review this code:\n\n${code}`
      }
    }]
  })
);

//通信协议
const transport = new StdioServerTransport()

//连接
server.connect(transport)